<?php

namespace App\Http\Controllers\Company;

use App\Facades\Style;
use App\Http\Requests\Company\License\CreateRequest;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyCertification;
use Illuminate\Http\Request;

class CompanyCertificationController extends CommonController{
    /**
     * 营业执照
     * @param Request $request
     * @return mixed
     */
    public function licenseSub(Request $request){
        $user = auth()->guard('company')->user();
        $companyBasic = CompanyBasic::where('uid', $user->uid)->select('company_name', 'uid', 'id')->first();
        if(empty($companyBasic)){
            return redirect('company/login');
        }
        if(!empty($request->afresh)){
            return Style::view('company.licenseSub', compact('companyBasic'));
        }
        $companyCertification = CompanyCertification::where('uid', $user->uid)->select('status', 'license', 'remark')->first();
        if(empty($companyCertification)){
            return Style::view('company.licenseSub', compact('companyBasic'));
        }elseif($companyCertification->status == '0'){
            return Style::view('company.licenseCheck');
        }else{
            $companyCertification->license = asset('/upload/companylicense/' . $companyCertification->license);
            return Style::view('company.licenseResult', compact('companyCertification'));
        }
    }

    /**
     * 营业执照上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function licenseUpload(Request $request){
        $input = $request->only('uid', 'com_id', 'company_name');
        if(empty($input['uid']) || empty($input['com_id']) || empty($input['company_name'])){
            return response()->json(['error' => 306, 'info' => "该企业可能不存在！"]);
        }
        if($request->hasFile('file')){
            $file = $request->file('file');
            $entension = strtolower($file->getClientOriginalExtension());
            //$mimeTye = $file -> getMimeType();
            $clientName = $file->getClientOriginalName();//客服端文件名
            if($file->isValid()){
                if(!in_array($entension, array('jpg', 'gif', 'png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型只能为jpg、gif、png！"]);
                }else{
                    $userInfo = auth()->guard('company')->user();
                    $company_basic = CompanyBasic::where('uid', $userInfo->uid)->where('id', $request->com_id)->first();
                    if(empty($company_basic)){
                        return response()->json(['error' => 404, 'info' => "没有找到基本信息！"]);
                    }
                    $fileurl = date("Ym") . '/';
                    $filename = time() . strtolower(str_random(5)) . '.' . $entension;
                    $request->file('file')->move(public_path() . '/upload/companylicense/' . $fileurl, $filename);
                    //检查用户有没上传执照
                    $companyCertification = CompanyCertification::where('uid', $request->uid)->where('com_id', $request->com_id)->first(['id', 'license']);
                    $input['license'] = $fileurl . $filename;
                    $input['status'] = 0;
                    if(empty($companyCertification)){
                        CompanyCertification::create($input);
                    }else{
                        @unlink(public_path() . '/upload/companylicense/' . $companyCertification->license);
                        CompanyCertification::where('id', $companyCertification->id)->update($input);
                    }
                    //设置企业信息为审核状态
                    CompanyBasic::where('uid', $userInfo->uid)->where('id', $request->com_id)->update(['cert_status' => 0]);
                    return response()->json(['error' => 200, 'info' => "营业执照上传成功",'redirect'=>'/company/license_sub']);
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件！"]);
        }
    }
}